package com.example.agriapp.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.agriapp.R;
import com.example.agriapp.data.Purchase;
import com.example.agriapp.model.bean.Product;
import com.example.agriapp.view.activity.ProductDetailsActivity;

import java.util.List;

public class CaiGouAdapter extends RecyclerView.Adapter<CaiGouAdapter.ProductViewHolder> {

    private List<Purchase> productList;
    private Context context;

    public CaiGouAdapter(Context context, List<Purchase> productList) {
        this.context = context;
        this.productList = productList;
    }

    @NonNull
    @Override
    public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_product_liulan, parent, false);
        return new ProductViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ProductViewHolder holder, int position) {
        Purchase product = productList.get(position);

        // 设置商品信息
        holder.tvTitle.setText(product.getTitle());
        holder.tvPrice.setText(product.getPrice()+"");
//        holder.tvRegion.setText(product.getRegion());
//        holder.tvPhone.setText(product.getPhone());

        // 使用Glide加载商品图片
        Glide.with(context)
                .load(product.getImageUrl())
                .placeholder(R.drawable.ic_caigou)
                .error(R.drawable.ic_caigou)
                .into(holder.ivProduct);

        // 点击拨打电话
        holder.tvPhone.setOnClickListener(v -> {
//            Intent intent = new Intent(Intent.ACTION_DIAL);
//            intent.setData(Uri.parse("tel:" + product.getPhone()));
//            context.startActivity(intent);
        });
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 跳转到商品详情页面
//                Intent intent = new Intent(context, ProductDetailsActivity.class);
//                intent.putExtra("product", product);
//                context.startActivity(intent);
            }
        });

    }



    @Override
    public int getItemCount() {
        return productList.size();
    }

    static class ProductViewHolder extends RecyclerView.ViewHolder {
        ImageView ivProduct;
        TextView tvTitle;
        TextView tvPrice;
        TextView tvRegion;
        TextView tvPhone;

        public ProductViewHolder(@NonNull View itemView) {
            super(itemView);
            ivProduct = itemView.findViewById(R.id.iv_product);
            tvTitle = itemView.findViewById(R.id.tv_title);
            tvPrice = itemView.findViewById(R.id.tv_price);
            tvRegion = itemView.findViewById(R.id.tv_region);
            tvPhone = itemView.findViewById(R.id.tv_phone);
        }
    }
}